"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具按文件名查找文件所在路径
Create At   : 2023/4/14
Owner       : opentestcase019
Description :
    1.按文件名查找文件所在路径，指定-p path和-n name参数为正常值
    2.按文件名查找文件所在路径，指定-p path参数为不存在的路径和包含特殊字符的路径
    3.按文件名查找文件所在路径，指定-n name参数为不符合规范的文件名
    4.按文件名查找文件所在路径，指定-U UDS:socket_domain参数为正常值
    5.按文件名查找文件所在路径，指定错误的-U UDS:socket_domain参数
    6.按文件名查找文件所在路径，指定权限不足的socket_domain
    7.在备机点执行按文件名查找文件所在路径
    8.环境清理
Expect      :
    1.查找文件成功
    2.合理报错
    3.合理报错
    4.查找文件成功
    5.合理报错Failed to get uds connection.
    6.合理报错Failed to get uds connection.
    7.查找文件成功
    8.清理成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()
        self.file_name = "file_dsscmd_0026"
        self.file_name_63chars = \
            "file_dsscmd_0026_file_dsscmd_0026_file_dsscmd_0026_file_63chars"
        self.file_name_64chars = \
            "file_dsscmd_0026_file_dsscmd_0026_file_dsscmd_0026_file_64chars1"
        self.file_name_numbers = "0026"
        self.file_name_letters = "filedsscmd"
        self.vgname_64chars = \
            "data_64_chars_data_64_chars_data_64_chars_data_64_chars_data_64c"
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg, "获取卷组名失败")
        self.vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(self.vgname)
        self.file_path = os.path.join(self.vgname, self.file_name)
        self.file_path_63chars = os.path.join(self.vgname,
                                              self.file_name_63chars)
        self.file_path_numbers = os.path.join(self.vgname,
                                              self.file_name_numbers)
        self.file_path_letters = os.path.join(self.vgname,
                                              self.file_name_letters)

    def test_sharestorage_tool(self):
        step1 = "-----step1:按文件名查找文件所在路径，指定-p path和-n name参数" \
                "为正常值;expect:查找文件成功-----"
        self.logger.info(step1)
        cmd_list = [self.file_path, self.file_path_63chars,
                    self.file_path_numbers, self.file_path_letters]
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd touch -p +{cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn(f"Succeed to create file, name is +{cmd}.",
                          execute_msg, "执行失败" + step1)
        name_list = [self.file_name, self.file_name_63chars,
                     self.file_name_numbers, self.file_name_letters]
        for name in name_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd find -p +{self.vgname} -n {name}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn(os.path.join(self.vgname, name),
                          execute_msg, "执行失败" + step1)

        step2 = "-----step2:按文件名查找文件所在路径，指定-p path参数为" \
                "不存在的路径和包含特殊字符的路径;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = [f'+{self.vgname}_not_exists',
                    f'+{self.vgname}$', f'+{self.vgname}#',
                    f'+{self.vgname_64chars}', f' {self.vgname}']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd find -p {cmd} -n {self.file_name}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == cmd_list[0]:
                self.assertIn(f"Failed to find traverse path "
                              f"+{self.vgname}_not_exists.",
                              execute_msg, "执行失败" + step2)
            elif cmd == cmd_list[3]:
                self.assertIn(" Path  decode error name length should less "
                              "than 64", execute_msg, "执行失败" + step2)
            elif cmd == cmd_list[-1]:
                self.assertIn("decode error , path should start with +.",
                              execute_msg, "执行失败" + step2)
            else:
                self.assertIn("decode error , path should be "
                              "[0~9,a~z,A~Z,-,_,/,.]",
                              execute_msg, "执行失败" + step2)

        step3 = "-----step3:按文件名查找文件所在路径，指定-n name参数为" \
                "不符合规范的文件名;expect:合理报错-----"
        self.logger.info(step3)
        cmd_list = [f'{self.file_name}@#', f'{self.file_name_64chars}',
                    f'{self.file_name}文件', f'{self.file_name}_not_exists']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd find -p +{self.vgname} -n {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == cmd_list[1]:
                self.assertIn("decode error , name is too long.",
                              execute_msg, "执行失败" + step2)
            elif cmd == cmd_list[-1]:
                self.assertIn("", execute_msg, "执行失败" + step2)
            else:
                self.assertIn("decode error , name should be "
                              "[0~9,a~z,A~Z,-,_,.]",
                              execute_msg, "执行失败" + step2)

        step4 = "-----step4:按文件名查找文件所在路径，指定-U UDS:socket_domain" \
                "参数为正常值;expect:查找文件成功-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd find -p +{self.vgname} -n {self.file_name} " \
            f"-U {macro.UDS_PATH}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.file_path, execute_msg, "执行失败" + step4)

        step5 = "-----step5:按文件名查找文件所在路径，指定错误的-U UDS:socket_domain参数;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd find -p +{self.vgname} -n {self.file_name} " \
            f"-U {macro.UDS_PATH}.cfg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step5)

        step6 = "-----step6:按文件名查找文件所在路径，指定权限不足的socket_domain;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.UDS_PATH[4::]};" \
            f"dsscmd find -p +{self.vgname} -n {self.file_name} " \
            f"-U {macro.UDS_PATH};" \
            f"chmod 600 {macro.UDS_PATH[4::]}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step6)

        step7 = "-----step7:在备机点执行按文件名查找文件所在路径;expect:查找文件成功-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd find -p +{self.vgname} -n {self.file_name} " \
            f"-U {macro.UDS_PATH}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn(self.file_path, execute_msg, "执行失败" + step7)

    def tearDown(self):
        step8 = "-----step8:环境清理;expect:清理成功-----"
        self.logger.info(step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd rm -p +{self.file_path};" \
            f"dsscmd rm -p +{self.file_path_63chars};" \
            f"dsscmd rm -p +{self.file_path_numbers};" \
            f"dsscmd rm -p +{self.file_path_letters}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEquals(4, execute_msg.count("Succeed to remove device"),
                          "环境清理失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
